package sc.love.boot.es;

import com.alibaba.fastjson.JSONObject;
import sc.love.boot.util.HttpClientUtil;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MultithreadUploadKnowledgeNotice extends Thread {

    private int startRownum;

    private int endRownum;

    private Connection connection = null;

    public MultithreadUploadKnowledgeNotice(int startRownum, int endRownum, Connection connection) {
        super();
        this.startRownum = startRownum;
        this.endRownum = endRownum;
        this.connection = connection;
    }

    public void run() {
        Statement statement = null;
        ResultSet rs = null;
        StringBuffer sb = new StringBuffer();
        if (connection != null) {
            try {
                // 读取数据
                statement = connection.createStatement();
                String sql = " select c.* from (" + " select rownum rn,a.* " +
                        "from MSGCENTER.A_KNOWLEDGE_NOTICE a where a.del_flag = '0' ) c "
                        + "where c.rn between " + startRownum + " and " + endRownum + " order by c.id";
                rs = statement.executeQuery(sql);
                while (rs.next()) {
                    KnowledgeNotice knowledgeBody = new KnowledgeNotice();
                    knowledgeBody.setTitle(rs.getString("TITLE"));
                    knowledgeBody.setId(rs.getLong("ID"));
                    long id = rs.getLong("ID");
                    knowledgeBody.setAllowNext(rs.getString("ALLOW_NEXT"));
                    knowledgeBody.setHaveExam(rs.getString("HAVE_EXAM"));
                    knowledgeBody.setExamId(rs.getString("EXAM_ID"));
                    knowledgeBody.setCreateUser(rs.getLong("CREATE_USER"));
                    knowledgeBody.setDimension(rs.getString("DIMENSION"));
                    knowledgeBody.setExpiresTime(rs.getDate("EXPIRES_TIME"));
                    knowledgeBody.setRemark(rs.getString("REMARK"));
                    knowledgeBody.setNoticeText(rs.getString("NOTICE_TEXT"));
                    Date create_time = rs.getDate("CREATE_TIME");
                    knowledgeBody.setCreateTime(create_time);
                    knowledgeBody.setDelFlag(rs.getString("DEL_FLAG"));
                    knowledgeBody.setUpdateTime(null);
                    knowledgeBody.setExamStatus(rs.getString("EXAM_STATUS"));


                    String staffSql = "select * from MSGCENTER.a_Know_Staff_Rel a where a.del_flag = '0' and a.know_noti_id =  " + id;
                    ResultSet rss = null;
                    Statement statement1 = connection.createStatement();
                    rss = statement1.executeQuery(staffSql);
                    List<KnowStaffRel> ks = new ArrayList<>();
                    while (rss.next()) {
                        KnowStaffRel staffRelDto = new KnowStaffRel();
                        staffRelDto.setKNOW_NOTI_ID(rss.getInt("KNOW_NOTI_ID"));
                        staffRelDto.setEXPIRES_TIME(rss.getDate("EXPIRES_TIME"));
                        staffRelDto.setHAVE_EXAM(rss.getString("HAVE_EXAM"));
                        staffRelDto.setID(rss.getLong("ID"));
                        staffRelDto.setSTAFF_ID(rss.getLong("STAFF_ID"));
                        staffRelDto.setTARGET_APP_CODE(rss.getString("TARGET_APP_CODE"));
                        staffRelDto.setDEL_FLAG(rss.getString("DEL_FLAG"));
                        staffRelDto.setREAD_STATUS(rss.getShort("READ_STATUS"));
                        staffRelDto.setCREATE_TIME(rss.getDate("CREATE_TIME"));
                        staffRelDto.setREAD_TIME(rss.getDate("READ_TIME"));
                        staffRelDto.setUPDATE_TIME(rss.getDate("UPDATE_TIME"));
                        staffRelDto.setEXAM_STATUS(rss.getString("EXAM_STATUS"));
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
                        //Date exam_time = rss.getDate("EXAM_TIME");
                        staffRelDto.setEXAM_TIME(null);
                        staffRelDto.setEXAM_SCORE(rss.getString("EXAM_SCORE"));

                        ks.add(staffRelDto);
                    }
                    knowledgeBody.setStaffCodeList(ks);

                    String s = JSONObject.toJSONString(knowledgeBody);

                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    String format = simpleDateFormat.format(create_time);
                    String index="crm-ubp-message-"+format;
                    System.out.println(s);
/*                    try {
                        String documentByManually = createDocumentByManually("", "", "Basic ZWxhc3RpYzplczQzMjE=", index, "knowledgenotice", id + "", s);
                        System.out.println(id+":"+documentByManually);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }*/

                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {

                // try {
                // if (rs != null)
                // rs.close();
                // if (statement != null)
                // statement.close();
                // if (connection != null)
                // connection.close();
                // } catch (SQLException e) {
                // e.printStackTrace();
                // }

            }
        }
    }

    public static final int outTimes = 3000;
    public static String createDocumentByManually(String ip,String port,String password,String index,String name,String id,String message) throws Exception{
        String url = "http://132.252.128.143:9200/"+index;

        if(name!=null){
            url = url + "/"+name;
            if(id!=null){
                url = url + "/"+id;
            }
        }
        Map headDatas = new HashMap();
        if(password!=null&&!"".equals(password)){
            headDatas.put("Authorization",password);
        }
        String returnMsg = HttpClientUtil.doPost(url, message, "UTF-8", outTimes, headDatas);
        JSONObject returnJson =  JSONObject.parseObject(returnMsg);
        if(returnJson.getJSONObject("error")!=null && !returnJson.getJSONObject("error").isEmpty()){
            throw new Exception(returnJson.getJSONObject("error").toJSONString());
        }
        return returnMsg;
    }

}
